/**
 * 
 */
package cn.mxz.robots.province;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;

import net.sf.json.JSONObject;

import org.apache.poi.hwpf.extractor.WordExtractor;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import cn.mxz.crawler_single.basic.Request;
import cn.mxz.rds.pojo.GaoKaoProvince;
import cn.mxz.rds.pojo.GaoKaoRank;
import cn.mxz.rds.service.GaoKaoProvinceService;
import cn.mxz.rds.service.GaoKaoRankService;
import cn.mxz.robots.basic.local.Exam;
import cn.mxz.robots.basic.local.IRank;
import cn.mxz.robots.pojo.Student;

/**
 * 浙江省
 * 排名:完成
 * 去年排名:完成
 * 
 * @author cuizhennan<cuizhennan@mouge.wang>
 * @author chenmin<chenmin@mouge.wang>
 * @since 2015-06-22
 */
public class Zhejiang extends Exam implements IRank
{
	public static void main(String[] args)
	{
		Student student = new Student("15330333150303", "tsn94746112", "6789", "123", "bw");
		Zhejiang gansu = new Zhejiang(student);
		System.out.println(gansu.getCaptcha());
		System.out.println("请输入验证码：");
		@SuppressWarnings("resource")
		String captcha = new Scanner(System.in).next();
		System.out.println(gansu.login(captcha));
	}

	public Zhejiang(Student student)
	{
		super(student);
	}

	@Override
	public String getCaptcha()
	{
		delCookie();
		// Request captchaRequest = new
		// Request("http://cx.zjzs.net/INC/VerifyCode.aspx?", mSha1);
		Request captchaRequest = new Request("http://gk.zjedu.gov.cn:6060/INC/VerifyCode.aspx", mSha1);
		captchaRequest.setCharSet("utf-8");
		// captchaRequest.addHeader("Referer",
		// "http://cx.zjzs.net/exam/GKCX2015/");
		captchaRequest.addHeader("Referer", "http://gk.zjedu.gov.cn:6060/exam/gkcx2015/");
		captchaRequest.addHeader("Host", "gk.zjedu.gov.cn:6060");
		return curlFile(captchaRequest);
	}

	@Override
	public String login(String captcha)
	{
		// Request("http://cx.zjzs.net/exam/GKCX2015/resault.aspx", mSha1);
		Request loginRequest = new Request("http://gk.zjedu.gov.cn:6060/exam/gkcx2015/resault.aspx", mSha1);
		loginRequest.setCharSet("utf-8");
		// "http://cx.zjzs.net/exam/GKCX2015/");
		loginRequest.addHeader("Referer", "http://gk.zjedu.gov.cn:6060/exam/gkcx2015/");
		loginRequest.addHeader("Origin", "http://gk.zjedu.gov.cn:6060");
		loginRequest.addHeader("Host", "gk.zjedu.gov.cn:6060");
		loginRequest.addParams("ZKZH", mStudent.getStudentId());
		loginRequest.addParams("PASSWORD", mStudent.getStudentPwd());
		loginRequest.addParams("yzm", captcha);
		String loginHtml = curlPost(loginRequest);
		JSONObject resultObject = new JSONObject();

		if (loginHtml.contains("成绩查询结果"))
		{
			resultObject.put("result", LOGIN_SUCCESS);
			resultObject.put("data", loginParseHtml(loginHtml));
		}
		else if (loginHtml.contains("准考证号"))
		{
			resultObject.put("result", ERROR);
			resultObject.put("data", NOT_EXIST_USER);
		}
		else if (loginHtml.contains("信息的输入有误"))
		{
			resultObject.put("result", ERROR);
			resultObject.put("data", WRONG_USER_PASSWORD);
		}
		else if (loginHtml.contains("验证码输入"))
		{
			resultObject.put("result", ERROR);
			resultObject.put("data", WRONG_CAPTCHA);
		}
		else
		{
			System.out.println(loginHtml);
			resultObject.put("result", ERROR);
			resultObject.put("data", INTERNAL_SERVER_ERROR);
		}

		return resultObject.toString();
	}

	@Override
	public String loginParseHtml(String parseHtml)
	{
		Document loginDocument = Jsoup.parse(parseHtml);
		JSONObject result = new JSONObject();
		result.put("exam_id", mStudent.getStudentId());
		result.put("name", loginDocument.select("#XM").get(0).text());
		result.put("ranking", getRank(62, loginDocument.select("#TOT1").get(0).text(), mStudent.getType()));
		result.put("province", "浙江省");

		JSONObject scoreObject = new JSONObject();
		scoreObject.put("语文", loginDocument.select("#KM1").get(0).text());
		scoreObject.put("数学", loginDocument.select("#KM2").get(0).text());
		scoreObject.put("综合", loginDocument.select("#KM10").get(0).text());
		scoreObject.put("外语", loginDocument.select("#KM3").get(0).text());
		scoreObject.put("总分1", loginDocument.select("#TOT1").get(0).text());
		scoreObject.put("自选模块", loginDocument.select("#KM11").get(0).text());
		scoreObject.put("技术", loginDocument.select("#KM12").get(0).text());
		scoreObject.put("英语考生听力成绩", loginDocument.select("#YYTL").get(0).text());
		scoreObject.put("英语考生笔试成绩", loginDocument.select("#YYBS").get(0).text());
		scoreObject.put("特征分", loginDocument.select("#TZF").get(0).text());
		scoreObject.put("总分2", loginDocument.select("#TOT2").get(0).text());
		scoreObject.put("总分3", loginDocument.select("#TOT3").get(0).text());
		scoreObject.put("总分4", loginDocument.select("#TOT4").get(0).text());

		result.put("score", scoreObject.toString());

		return result.toString();
	}

	@Override
	public void initRank()
	{
		File file = new File("/home/chenmin/Desktop/wen.doc");

		try
		{
			FileInputStream fis = new FileInputStream(file);
			WordExtractor wordExtractor = new WordExtractor(fis);
			String[] paragraph = wordExtractor.getParagraphText();
			for (int i = 4; i < paragraph.length; i++)
			{
				if (paragraph[i].length() < 21 || paragraph[i].contains("2015年") || paragraph[i].contains("分数"))
					continue;
				String content = paragraph[i].replaceAll(" ", "");

				Integer scores = Integer.valueOf(content.substring(1, 4));
				Integer counters = Integer.valueOf(regexMatch(content, "\\│(\\d+)\\／"));
				Integer rankings = Integer.valueOf(regexMatch(content, "\\／(\\d+)\\│"));
				System.out.println(scores + ":" + counters + ":" + rankings);

				GaoKaoProvinceService provinceService = new GaoKaoProvinceService();
				GaoKaoProvince province = provinceService.get(62);
				GaoKaoRankService rankService = new GaoKaoRankService();
				GaoKaoRank rank = new GaoKaoRank();
				rank.setScore(scores);
				rank.setCount(counters);
				rank.setType(WEN_KE);
				rank.setRanking(rankings);
				rank.setGaoKaoProvince(province);
				rankService.save(rank);
			}
		}
		catch (FileNotFoundException e)
		{
			e.printStackTrace();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	@Override
	public void lastYearRank()
	{
		// 理 http://www.gxeduw.com/gaokao/gkcjpm/zj/2014/106987.html
		// 文 http://www.gxeduw.com/gaokao/gkcjpm/zj/2014/106988.html
		Request request = new Request("http://www.gxeduw.com/gaokao/gkcjpm/zj/2014/106987.html", mSha1);
		Document result = Jsoup.parse(curlGet(request));

		Elements trs = result.select("table").get(0).select("tr");
		for (int i = 1; i < trs.size(); i++)
		{
			if(trs.get(i).select("td").get(0).text().replaceAll(" ", "").contains("分数"))
				continue;
			
			Integer scores = Integer.parseInt(trs.get(i).select("td").get(0).text().replaceAll(" ", "").replaceAll(">", ""));
			Integer counters = Integer.parseInt(trs.get(i).select("td").get(1).text());
			Integer rankings = Integer.parseInt(trs.get(i).select("td").get(2).text());

			System.out.println(scores + ":" + counters + ":" + rankings);

			GaoKaoProvinceService provinceService = new GaoKaoProvinceService();
			GaoKaoProvince province = provinceService.get(31);
			GaoKaoRankService rankService = new GaoKaoRankService();
			GaoKaoRank rank = new GaoKaoRank();
			rank.setScore(scores);
			rank.setCount(counters);
			rank.setType(LI_KE);
			rank.setRanking(rankings);
			rank.setGaoKaoProvince(province);
			rankService.save(rank);
		}
	}
}
